package tt.dz.egobus.servicemanager.controller.carservice;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tt.dz.egobus.servicemanager.base.controller.EntityController;
import tt.dz.egobus.servicemanager.base.service.ExtendBaseService;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.entities.BusService;
import tt.dz.egobus.servicemanager.service.BusRegisterServiceService;
import tt.dz.egobus.servicemanager.service.BusServiceService;
import tt.dz.egobus.servicemanager.vo.BusServiceViewVo;
import tt.dz.egobus.servicemanager.vo.JsonBack;
import tt.dz.egobus.vo.KV;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.emuns.OrderBusinessCode.HALF_DAY_TAKE;
import static tt.dz.egobus.emuns.OrderBusinessCode.QUERY_DAY_TAKE;
import static tt.dz.egobus.emuns.OrderBusinessCode.QUERY_TIME_TAKE;
import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.BUS_SERVICE_EXIST_ERROR;
import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.BUS_SERVICE_EXIST_RELATIVE;

/**
 * @author Anson
 * @date 17-3-15
 * @name 车辆服务
 * @since 1.0.1
 */
@RestController
@RequestMapping("/busService")
public class BusServiceController extends EntityController<BusService, BusServiceViewVo, String> {

    @Autowired
    @Qualifier("busServiceServiceImpl")
    private BusServiceService service;

    @Autowired
    @Qualifier("busRegisterServiceServiceImpl")
    private BusRegisterServiceService registerService;

    @Override
    protected ExtendBaseService<BusService, String> getSelfService() {
        return this.service;
    }

    @Override
    protected BusServiceViewVo newViewVo() {
        return new BusServiceViewVo();
    }

    /**
     * @author Anson
     * @date 17-4-14
     * @name 查询当前系统服务关心的服务号
     * @since 1.0.4
     */
    @RequestMapping("/queryBusinessCodes")
    public JsonBack<List<KV<Integer, String>>> queryBusinessCodes() {
        List<KV<Integer, String>> businessCodes = new ArrayList<KV<Integer, String>>();
        businessCodes.add(new KV<Integer, String>(QUERY_DAY_TAKE.getCode(), QUERY_DAY_TAKE.getMsg()));
        businessCodes.add(new KV<Integer, String>(QUERY_TIME_TAKE.getCode(), QUERY_TIME_TAKE.getMsg()));
        businessCodes.add(new KV<Integer, String>(HALF_DAY_TAKE.getCode(), HALF_DAY_TAKE.getMsg()));
        return JsonBack.success(businessCodes);
    }

    /**
     * @param busService 车辆服务
     * @author Anson
     * @date 17-3-28
     * @name 新增的时候判断当前业务服务号是否已经被占用了
     * @since 1.0.3
     */
    @Override
    protected void preInsertHandler(BusService busService) throws BusinessException {
        if (this.service.isExistBusinessCode(busService.getBusinessCode(), null)) {
            throw new BusinessException(BUS_SERVICE_EXIST_ERROR.getCodeStatus());
        }
    }

    /**
     * @param busService
     * @author Anson
     * @date 17-3-28
     * @name 更新的时候判断当前业务服务号是否已经被占用了
     * @since 1.0.3
     */
    @Override
    protected void preUpdateHandler(BusService busService) throws BusinessException {
        if (this.service.isExistBusinessCode(busService.getBusinessCode(), busService.getId())) {
            throw new BusinessException(BUS_SERVICE_EXIST_ERROR.getCodeStatus());
        }
    }

    /**
     * @param busService 当前的车辆服务
     * @author Anson
     * @date 17-4-6
     * @name 车辆服务更新了当前状态，对应的下级服务注册的时候需要更新
     * @since 1.0.4
     */
    @Override
    protected void postUpdateHandler(BusService busService) throws BusinessException {
        if (busService == null || StringUtils.isEmpty(busService.getId())) {
            return;
        }
        this.registerService.updateIsValidateStatus(busService.isActive(), null, busService.getId());
    }

    /**
     * @param id 当前的车辆服务id
     * @author Anson
     * @date 17-4-6
     * @name 删除当前车辆服务，判断当前是佛存在注册服务，若存在服务注册，则不能删除
     * @since 1.0.4
     */
    @Override
    protected void preDeleteHandler(String id) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eq#bus_service_id", id);
        int count = this.registerService.count(params);
        if (count > 0) {
            throw new BusinessException(BUS_SERVICE_EXIST_RELATIVE.getCodeStatus());
        }
    }
}
